package com.supor.aiot.common.app;

import android.content.Context;

import com.alibaba.android.arouter.launcher.ARouter;
import com.android.baseconfig.base.BaseConfigApplication;
import com.android.baseconfig.base.BaseSharedPreferences;
import com.android.baseconfig.base.CrashHandler;
import com.android.baseconfig.common.utils.Logc;
import com.android.baseconfig.common.utils.SystemUtils;
import com.android.baseconfig.data.AppDelegate;
import com.android.baseconfig.constact.AppGlobalHost;
import com.android.baseconfig.data.http.AppNetDelegate;
import com.android.baseconfig.data.http.okhttp.OkHttpManager;
import com.cmic.sso.sdk.auth.AuthnHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.supor.aiot.common.ui.view.MyRefreshFooter;

import io.reactivex.rxjava3.plugins.RxJavaPlugins;

public class SuporApplication extends BaseConfigApplication {
    static {
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                return new MyRefreshFooter(context);
            }
        });
    }

    private static final String TAG = SuporApplication.class.getSimpleName();
    /**
     * 是否同意权限申请：0-未同意|1-已同意
     */
    int authPrivacyPolicy = 0;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        AppInit.getInstance().setup(this, null);
    }

    @Override
    public void onCreate() {
        // Logc.d("onCreate: start");
        String processName = SystemUtils.getProcessName(this, android.os.Process.myPid());
        if (processName.equals(this.getPackageName())) {
            super.onCreate();
            AppAgent.setApp(this);

            authPrivacyPolicy = BaseSharedPreferences.getInt(SuporApplication.getInstance(),
                    Constants.Key.param_authPrivacyPolicy, 0);

            boolean debugMode = true;//BaseSharedPreferences.getBoolean(this, Constants.Key.param_debug_mode, false);
            // if (SystemUtils.isApkInDebug(this) || debugMode) {
                // 这两行必须写在init之前，否则这些配置在init过程中将无效
                ARouter.openLog();     // 打印日志
                ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
                AuthnHelper.setDebugMode(true);
                AppNetDelegate.setLogger(true);
                Logc.setLogLevel(Logc.LogLevel.LEVEL_VERBOSE);
                AppAgent.isPublish = false;
            // } else {
            //     AuthnHelper.setDebugMode(false);
            //     AppNetDelegate.setLogger(false);
            //     Logc.setLogLevel(Logc.LogLevel.LEVEL_WARN);
            //     AppAgent.isPublish = true;
            // }
            Logc.i("debugMode= " + debugMode);

            int saveServer = BaseSharedPreferences.getInt(this, Constants.Key.param_server, -1);
            if (saveServer == -1) {
                BaseSharedPreferences.setInt(this, Constants.Key.param_server, AppAgent.currentServer);
            } else {
                AppAgent.currentServer = saveServer;
            }
            Logc.i("currentServer= " + AppAgent.currentServer);
            initOKhttp();

            // debug模式下crash不上传
            if (debugMode) {
                CrashHandler crashHandler = CrashHandler.getInstance();
                crashHandler.init(getApplicationContext());
            }

            if (authPrivacyPolicy != 0) {
                AppInit.getInstance().onCreate();
            }

        }
    }

    @Override
    protected void initOKhttp() {
        super.initOKhttp();
        // int currentServer = BaseSharedPreferences.getInt(this, Constants.Key.param_server);
        // int currentServer = 1;
        if (AppAgent.currentServer == 0) {
            AppDelegate.setHost(Constants.APP_RELEASE_URL);
            AppDelegate.setCommHost(Constants.RELEASE_COMMUNITY_HOST);
        } else if (AppAgent.currentServer == 1) {
            AppDelegate.setHost(Constants.APP_PRE_URL);
            AppDelegate.setCommHost(Constants.PRE_COMMUNITY_HOST);
        } else if (AppAgent.currentServer == 2) {
            AppDelegate.setHost(Constants.APP_TEST_URL);
//            AppDelegate.setCommHost(Constants.TEST_COMMUNITY_HOST);
            AppGlobalHost.setNormalCommHost(Constants.TEST_COMMUNITY_HOST);

        } else {
            return;
        }
        //初始化基础模块
        AppDelegate.init(this);
        if (AppAgent.isPublish) {
            OkHttpManager.setNoProxy();
        }

        RxJavaPlugins.setErrorHandler(throwable -> {
            Logc.i("setErrorHandler: " + throwable);
        });
    }
}
